<?php

namespace app\api\controller;

use app\common\model\Member as MemberModel;
use think\App;
use think\cache\driver\Redis;
use think\Collection;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\facade\Db;
use think\Response;
use think\facade\Request;
use app\common\common;
use app\api\server\Token;
use app\api\model\MemberLevel as MemberLevelModel;
use app\common\model\Statistics as StatisticsModel;
use app\api\server\Release;

class Baek extends Collection
{
    protected function initialize()
    {
        //释放矿机
        if(DIMINSHING == 1){
            common::Diminishing_release();
            die;
        }
        
        //每日回购5%
        if(NETWORK == 1){
            common::network();
            die;
        }

        $this->controller = $this->request->controller();
        $action = $this->request->action();
        $app = app('http')->getName();
        $this->common = new common;
        $token = Request::header('token');
        $this->language = Request::header('language') ?? 'english';;
        $this->redis = new Redis(config('cache.stores.redis'));

        $http = $this->remove_http(Request::instance()->domain());
        $domain_bind = config('app.domain_bind');

        $api = 0;
        foreach ($domain_bind as $k => $v){
            if($v == 'api' && $http == $k) $api = 1;
        }

        if($api != 1) abort(404, '页面异常');

        //通过Login进来的跳过登录验证
        if($this->controller != 'Login'){
            
            if($this->controller != 'task' && empty($token)){
                header("Location: /chain");
                die;
            }
            
//            if(empty($token)) abort(404, '页面异常');
            //验证token
            $res = (new Token())->chekToken($token);
            if($res['code'] == 2 || empty($res)){
                api_error('','Login Expired',$this->language,411);
            }else{
                $this->mid = $res['data']->id;//用户ID
                //查询用户详情
                $member = MemberModel::getDetails($this->mid);
                if($res['data']->type == 0) api_error('','Not yet registered',$this->language,411);

                //控制频率
                $upgrade_ping = $this->redis->get('upgrade'.$this->mid);
                //5秒一次触发
                if(empty($upgrade_ping)){
                    $this->redis->set('upgrade'.$this->mid,1,5);
                    //用户升级
                    MemberLevelModel::upgrade($this->mid);
                }
            }
        }
    }

    private function remove_http($url) {
       $disallowed = array('http://', 'https://');
       foreach($disallowed as $d) {
          if(strpos($url, $d) === 0) {
             return str_replace($d, '', $url);
          }
        }
        return $url;
    }

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }


}